<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Frontend extends MX_Controller {
    
    function __construct() {

        parent::__construct();
        $this->load->library('category');
        $this->load->library('session');
        if(empty($this->cart))$this->load->library('cart');
        if(empty($this->ticket_model))$this->load->model('ticket/ticket_model');
        $this->category->build_option_data(1,__('categories'),0);        
        
    }
    
    function change_language($lang='fr'){
        if($lang=='fr'){
            $this->session->set_userdata('lang','fr');
        }else{
            $this->session->set_userdata('lang','en');
        }
        $desination = $this->session->userdata('current_url')!=NULL?$this->session->userdata('current_url'):  url();
        if(strpos('change_language',$desination)===false) redirect($desination);
        else redirect('/');
    }
    
    function index() {
        $this->session->set_userdata('current_url', current_url());        
        $this->frontend_dispatcher('ticket');
    }
    
    public function frontend_dispatcher($controller_name = '', $method_name = '', $para = '') 
    {
        $this->session->set_userdata('current_url', current_url());
        $main_content_method = '';
        //$method_params = $para;//array();
        $layout = 'frontend_layout/frontend';
        $view_data = array();
        switch ($controller_name) 
        {
            case 'ticket' :
                if(empty($method_name))$main_content_method = 'ticket/list_ticket';
                else $main_content_method = 'ticket/'.$method_name;
                if($method_name=='detail' && $this->uri->segment(3)){
                    $id = $this->uri->segment(3);
                    if(!empty($id) && !is_numeric($id)) {
                        $get_one_ticket = $this->ticket_model->get_ticket(array('id'=>$id));
                        if(!empty($get_one_ticket)){
                            $get_one_ticket = $get_one_ticket[0];                    
                            $view_data['title'] = $get_one_ticket->meta_title;
                            $view_data['keywords'] = $get_one_ticket->meta_keyword;
                            $view_data['description']= $get_one_ticket->meta_description;
                        }
                    }
                }
                else{
                    $view_data['title'] = __('home_ticket_list');
                    $view_data['keywords'] = DEFAULT_KEYWORDS;
                    $view_data['description']= DEFAULT_DESCRIPTION;
                }
                
                
                break;
            
            case 'shopping' : $main_content_method = 'shopping/show_cart';
                    $view_data['title'] = __('home_shopping_cart');
                    $view_data['keywords'] = DEFAULT_KEYWORDS;
                    $view_data['description']= DEFAULT_DESCRIPTION;
                break;
            case 'paypal_success' : $main_content_method = 'paypal/success';
                $view_data['title'] = __('home_payment_success');
                    $view_data['keywords'] = DEFAULT_KEYWORDS;
                    $view_data['description']= DEFAULT_DESCRIPTION;
                break;
            case 'paypal_cancel' : $main_content_method = 'paypal/cancel';
                    $view_data['title'] = __('home_payment_canceled');
                    $view_data['keywords'] = DEFAULT_KEYWORDS;
                    $view_data['description']= DEFAULT_DESCRIPTION;
                break;
            case 'paypal_checkout' : 
                $view_data['title'] = __('home_payment_checkout');
                    $view_data['keywords'] = DEFAULT_KEYWORDS;
                    $view_data['description']= DEFAULT_DESCRIPTION;
                $view_data['onloadbody'] = 1;
                $main_content_method = 'paypal/auto_form';                
                break;
            
        }

        
        
        $view_data['cart_total'] = $this->cart->total_items();
        $view_data['categories']    = modules::run('ticket/list_ticket_categories');
        $view_data['tags']    = modules::run('ticket/list_ticket_tags');
        $view_data['main_content'] = ($main_content_method !== '') ? modules::run($main_content_method) : '';//, $method_params)
        
        /*$meta_info = $this->get_meta_info();
        $view_data['title'] = $meta_info[0];
        $view_data['keywords'] = $meta_info[1];
        $view_data['description']= $meta_info[2];*/
        //var_dump($view_data);
        $view_data['slideshows_header'] = $this->ticket_model->get_top10ticket_bestseller();//get10TicketBestSeller;
        $this->load->view($layout, $view_data, FALSE);
    }
    
}
